package com.yjps.common.timer;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by jessy on 2015/8/29.
 */
public class NIOComparator {
    public void IOMethod(String TPATH) {
        long start = System.currentTimeMillis();
        DataOutputStream dos = null;
        try {
            dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(new File(TPATH))));
            for (int i = 0; i < 40000000; i++) {
                dos.writeInt(i);
            }
            if (dos != null) {
                dos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("1 : " + (end - start));

        start = System.currentTimeMillis();

        try {
            DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream(new File(TPATH))));
            for (int i = 0; i < 400000; i++) {
                dis.readInt();
            }
            if (dis != null) {
                dis.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        end = System.currentTimeMillis();
        System.out.println("2 : " + (end - start));
    }

    public void ByteMethod(String TPATH) {
        long start = System.currentTimeMillis();
        try {
            FileOutputStream fout = new FileOutputStream(new File(TPATH));
            FileChannel fc = fout.getChannel();//得到文件通道
            ByteBuffer byteBuffer = ByteBuffer.allocate(400000 * 4);
            for (int i = 0; i < 400000; i++) {
                byteBuffer.put(int2byte(i));
            }
            byteBuffer.flip();//准备写
            fc.write(byteBuffer);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        System.out.println("3 : " + (end - start));

        start = System.currentTimeMillis();

        try {

            FileChannel fc = new FileInputStream(TPATH).getChannel();

            MappedByteBuffer lib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

            lib.asIntBuffer();

            while (lib.hasRemaining()) {

                lib.get();

            }

            if (fc != null) {

                fc.close();

            }

        } catch (FileNotFoundException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        } catch (IOException e) {

// TODO Auto-generated catch block

            e.printStackTrace();

        }

        end = System.currentTimeMillis();

        System.out.println(end - start);


    }


    public static byte[] int2byte(int res) {

        byte[] targets = new byte[4];

        targets[3] = (byte) (res & 0xff);//最低位

        targets[2] = (byte) ((res >> 8) & 0xff);//次低位

        targets[1] = (byte) ((res >> 16) & 0xff);//次高位

        targets[0] = (byte) ((res >>> 24));//最高位，无符号右移

        return targets;

    }


    public static int byte2int(byte b1, byte b2, byte b3, byte b4) {

        return ((b1 & 0xff) << 24) | ((b2 & 0xff) << 16) | ((b3 & 0xff) << 8) | (b4 & 0xff);

    }


    public static void main(String[] args) {

        NIOComparator nio = new NIOComparator();

        nio.IOMethod("c:\\1.txt");

        nio.ByteMethod("c:\\2.txt");

        nio.ByteMethod("c:\\3.txt");

    }


}
